<?php
namespace app\home\controller;

use app\home\model\OrderModel;
use app\home\model\ExpressModel;
use think\Controller;
use think\Request;

class Order extends Controller
{
    public function __construct(Request $request = null)
    {
        $result = $this->_getBid();
        if(!$result){
            $this->success('未登录', 'index/login');
        }
    }
    /**
     * 订单类
     * @return \think\response\View
     *
     * 代付款
     */
    public function index()
    {
        $bid = $this->_getBid();
        $status = 1;
        $result = OrderModel::instance()->orderlist($bid,$status,'createtime desc');
        foreach($result as $k=>$v){
            $data = [];
            $data = $v;
            $data['status'] = '待付款';
            $v['createtime'] == 0 ? $data['createtime'] = '' :$data['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            $result->offsetSet($k,$data);
        }

        return view('order/index',['data'=>$result]);
    }
    /**
     * 代发货
     */
    public function delivergoods()
    {
        $bid = $this->_getBid();
        $status = 2;
        $result = OrderModel::instance()->orderlist($bid,$status,'paytime asc');
        
        $express = ExpressModel::instance()->exprlist($bid);

        foreach($result as $k=>$v){
            $data = [];
            $data = $v;
            $data['status'] = '待发货';
            $v['paytime'] == 0 ? $data['paytime'] = '' :$data['paytime'] = date('Y-m-d H:i:s',$v['paytime']);
            $result->offsetSet($k,$data);
        }
        return view('order/delivergoods',['data'=>$result,'express'=>$express]);
    }
    /**
     * 代收货
     */
    public function takegoods()
    {
        $bid = $this->_getBid();
        $status = 3;
        $result = OrderModel::instance()->orderlist($bid,$status,'sendtime desc');
//  print_r($result);
        foreach($result as $k=>$v){
//            var_dump($v['sendtime']+537600);
            $data = [];
            $data = $v;
            $data['status'] = '待收货';
            $v['sendtime'] == 0 ? $data['sendtime'] = '' :$data['sendtime'] = date('Y-m-d H:i:s',$v['sendtime']);

            if(time() >= $v['sendtime']+537600){
                db('goods_order')->where('status',3)->update(['status' => 4]);

            }
            $result->offsetSet($k,$data);
        }
        return view('order/takegoods',['data'=>$result]);
    }
    /**
     * 已完成
     */
    public function complete()
    {
        $bid = $this->_getBid();
        $status = 4;
        $result = OrderModel::instance()->orderlist($bid,$status,'taketime asc');

        foreach($result as $k=>$v){
            $data = [];
            $data = $v;
            $data['status'] = '已完成';
            $v['taketime'] == 0 ? $data['taketime'] = '' :$data['taketime'] = date('Y-m-d H:i:s',$v['taketime']);
            $result->offsetSet($k,$data);
        }
        return view('order/complete',['data'=>$result]);
    }
    /**
     * 已关闭
     */
    public function occlude()
    {
        $bid = $this->_getBid();
        $status = -1;
        $result = OrderModel::instance()->orderlist($bid,$status,'createtime desc');

        foreach($result as $k=>$v){
            $data = [];
            $data = $v;
            $data['status'] = '已关闭';
            $v['createtime'] == 0 ? $data['createtime'] = '' :$data['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            $result->offsetSet($k,$data);
        }
        return view('order/occlude',['data'=>$result]);
    }
    /**
     * 详情
     */
    public function details(Request $request){
        $orderid = $request->get('orderid');
        $bid = $this->_getBid();
        $result = OrderModel::instance()->details($bid,$orderid);
        return view('order/occlude',['data'=>$result]);
    }
    /**
     * 详情
     */
    public function show(Request $request){
        $orderid = $request->get('id');
        $result = OrderModel::instance()->showlist($orderid);
	$numz  = $totalpricez = 0;
//        foreach ($result['goods'] as &$val){
//            $numz +=  $val['num'];
//            $totalpricez += $val['total_price'];
//        }
        if($result['status'] == 1)$result['status'] = '待付款';
        if($result['status'] == 2)$result['status'] = '待发货';
        if($result['status'] == 3)$result['status'] = '待收货';
        if($result['status'] == 4)$result['status'] = '已完成';
        if($result['status'] == -1)$result['status'] = '已关闭';
        $result['createtime'] == 0 ? $result['createtime'] = '' : $result['createtime'] = date('Y-m-d h:i:s',$result['createtime']);
        $result['paytime'] == 0 ? $result['paytime'] = '' : $result['paytime'] = date('Y-m-d h:i:s',$result['paytime']);
        $result['sendtime'] == 0 ? $result['sendtime'] = '' : $result['sendtime'] = date('Y-m-d h:i:s',$result['sendtime']);
        $result['taketime'] == 0 ? $result['taketime'] = '' : $result['taketime'] = date('Y-m-d h:i:s',$result['taketime']);
        return view('order/show',['data'=>$result,'num'=>$numz,'totalprice'=>$totalpricez]);
    }
    /**
     * 添加快递
     */
    public function addexpress(Request $request){
        $data = $request->get();
        $result = OrderModel::instance()->addexpress($data);
       if($result > 0){
            $this->success('添加成功', 'order/index');
        }else{
            $this->error('新增失败');
        }
    
    }

 /**
     * 确认发货
     */
    public function affirmgoods(Request $request){
        $orderid = $request->get('id');
        $result = OrderModel::instance()->affirmgoods($orderid);
        return $result;

    }

    
}
